﻿//********************************************************************
// Author:      刘艳明
// Create Date: 2012-09-30
// Description: 
//
// Version:     1.0.0.0
// Change History:
// Date		    Who		      Changes Made
//--------------------------------------------------------------------
// Please Insert the Modiy record here!
//--------------------------------------------------------------------
//********************************************************************


using System;
using System.Collections.Generic;
using System.Data;
using MillerApp.CommonLib.DataAccess;
using MillerApp.DataContract.MillerDB;
using Npgsql;
using NpgsqlTypes;

namespace MillerApp.DataAccess.MillerDB
{
	/// <summary>
	/// 这个类提供了对Security ()表的数据库操作
	/// </summary>
	public static partial class SecurityManager
	{
		private static DbHelper db = DbHelper.CreateDatabase();

		static string insertSQL ="INSERT INTO security (listeddate,pysx,status,stockexchangecode,compcode,secshortname,seccode) VALUES (@listeddate,@pysx,@status,@stockexchangecode,@compcode,@secshortname,@seccode)";
	
		static string updateSQL ="Update security Set listeddate=@listeddate,pysx=@pysx,status=@status,stockexchangecode=@stockexchangecode,compcode=@compcode,secshortname=@secshortname,seccode=@seccode Where id=@id";

		static string deleteSQL ="DELETE FROM  security  Where id=@id";	
		
		#region codesmith生成的代码
		/// <summary>
		/// 获取SecurityEntity实体 
		/// </summary>
		/// <param name="listeddate">listeddate</param>
		/// <returns>SecurityEntity</returns>
		public static SecurityEntity GetSecurityEntity(DateTime listeddate)
		{
			string strSQL = "select * from security where listeddate=@listeddate";		
			IDbDataParameter para_listeddate = new NpgsqlParameter("listeddate", NpgsqlDbType.Date);
			para_listeddate.Value = listeddate;
			IDbDataParameter[] paras=new IDbDataParameter[]{para_listeddate};
			
			
			SecurityEntity entity=new SecurityEntity();
			using(System.Data.IDataReader reader=db.ExecuteReader(strSQL,paras))
			{
				if(reader.Read()==false) return null;
				LoadFromReader(reader, ref entity);
				return entity;
			}
		}		
		
		/// <summary>
		/// 更新SecurityEntity
		/// </summary>
		/// <param name="entity">Entity</param>	
		public static void UpdateSecurity(SecurityEntity entity)
		{
			if(entity==null)
			{
				throw new Exception("参数[entity]为[null]！");
			}
			IDbDataParameter[] paras =GenerateParas(entity);		
			db.ExecuteNonQuery(updateSQL,paras);
		}
		
		
				
        
        /// <summary>
		///  插入一条记录
		/// </summary>
		/// <param name="listeddate">listeddate</param>		
		public static void InsertSecurity(SecurityEntity entity)
		{
			if (entity == null)
            {
                throw new Exception("参数[entity]为[null]！");
            }
            IDbDataParameter[] paras = GenerateParas(entity);
            db.ExecuteNonQuery(insertSQL, paras);
		}
		
		/// <summary>
		///  删除一条记录
		/// </summary>
		/// <param name="listeddate">listeddate</param>		
		public static void DeleteSecurity(DateTime listeddate)
		{
			IDbDataParameter para_listeddate = new NpgsqlParameter("listeddate", NpgsqlDbType.Date);
			para_listeddate.Value = listeddate;
			IDbDataParameter[] paras=new IDbDataParameter[]{para_listeddate};
			db.ExecuteNonQuery(deleteSQL,paras);
		}

	
		
		/// <summary>
		/// 删除多条记录
		/// </summary>
		/// <param name="listeddates">listeddates</param>		
		public static void DeleteSecurity(List<DateTime> listeddates)
		{
			string strSQL = "DELETE FROM  security  Where listeddate in ({0}) ";
            string deleteIds = DbHelper.ConvertListToSQLString<DateTime>(listeddates);
            db.ExecuteNonQuery(string.Format(strSQL, deleteIds),null);
		}
		
		
	
		/// <summary>
		/// 根据分页条件获取Security数据集
		/// </summary>
		/// <param name="conditions">条件</param>	
		/// <param name="sortExpression">排序表达式，为空则默认为:listeddate</param>		
		/// <param name="pageIndex">开始页码：分页查询中的开始页码</param>
		/// <param name="pageSize">页宽：分页查询中的每页的记录数</param>
		/// <param name="recordCount">页宽：分页查询中的每页的记录数</param>		
		/// <returns>数据集(List)</returns>
		public static List<SecurityEntity> GetSecuritysByCond(string conditions,string sortExpression,int pageIndex,int pageSize,out int recordCount)
		{
			string viewName="security";
			return db.GetDataListByCond<SecurityEntity>
                (viewName, conditions, sortExpression, pageIndex, pageSize, out recordCount, LoadFromReader);
		}
				
		
		/// <summary>
		/// 根据条件获取Security数据集
		/// </summary>
		/// <param name="fields">查询的列(如果为空，则表示查询所有列)</param>	
		/// <param name="conditions">条件</param>	
		/// <param name="sortExpression">排序表达式，为空则默认为:listeddate</param>		
		/// <returns>数据集(DataTable)：<br/></returns>
		public static DataTable GetSecuritysByCond(string fields,string conditions,string sortExpression)
		{		
			string viewName="security";	
			return db.GetDataTableByCond(viewName, fields, conditions, sortExpression);		
		}
		#region private method
		/// <summary>
        /// 获取SecurityEntity实体数据更新参数
        /// </summary>
        /// <param name="entity">CompanyEntity</param>
        /// <returns></returns>
        private static IDbDataParameter[] GenerateParas(SecurityEntity entity)
        { 
			IDbDataParameter para_listeddate = new NpgsqlParameter("listeddate", NpgsqlDbType.Date);
			para_listeddate.Value = entity.listeddate;
			
			IDbDataParameter para_pysx = new NpgsqlParameter("pysx", NpgsqlDbType.Varchar);
			para_pysx.Value = entity.pysx;
			
			IDbDataParameter para_status = new NpgsqlParameter("status", NpgsqlDbType.Integer);
			para_status.Value = entity.status;
			
			IDbDataParameter para_id = new NpgsqlParameter("id", NpgsqlDbType.Integer);
			para_id.Value = entity.id;
			
			IDbDataParameter para_stockexchangecode = new NpgsqlParameter("stockexchangecode", NpgsqlDbType.Varchar);
			para_stockexchangecode.Value = entity.stockexchangecode;
			
			IDbDataParameter para_compcode = new NpgsqlParameter("compcode", NpgsqlDbType.Varchar);
			para_compcode.Value = entity.compcode;
			
			IDbDataParameter para_secshortname = new NpgsqlParameter("secshortname", NpgsqlDbType.Varchar);
			para_secshortname.Value = entity.secshortname;
			
			IDbDataParameter para_seccode = new NpgsqlParameter("seccode", NpgsqlDbType.Varchar);
			para_seccode.Value = entity.seccode;
			
			return new IDbDataParameter[]{para_listeddate,para_pysx,para_status,para_id,para_stockexchangecode,para_compcode,para_secshortname,para_seccode};	
        }
		
		/// <summary>
		/// 绑定数据到业务实体
		/// </summary>
		/// <param name="reader">DataReader</param>
		/// <param name="entity">业务实体</param>
		private static void LoadFromReader(IDataReader reader,ref SecurityEntity entity)
		{			
			if (reader != null && !reader.IsClosed)
			{
				//entity.Id = reader.GetDateTime(0);
                if(!Convert.IsDBNull(reader["listeddate"]))entity.listeddate = (DateTime)reader["listeddate"];
                if(!Convert.IsDBNull(reader["pysx"]))entity.pysx = reader["pysx"].ToString();
                if(!Convert.IsDBNull(reader["status"]))entity.status = (int)reader["status"];
                if(!Convert.IsDBNull(reader["id"]))entity.id = (int)reader["id"];
                if(!Convert.IsDBNull(reader["stockexchangecode"]))entity.stockexchangecode = reader["stockexchangecode"].ToString();
                if(!Convert.IsDBNull(reader["compcode"]))entity.compcode = reader["compcode"].ToString();
                if(!Convert.IsDBNull(reader["secshortname"]))entity.secshortname = reader["secshortname"].ToString();
                if(!Convert.IsDBNull(reader["seccode"]))entity.seccode = reader["seccode"].ToString();
			}
			
		}
		#endregion	
		
		#endregion
		
		#region 手工增加的代码
       
        #endregion
	}
}
